package com.qd.hik.service;

import com.cdqidi.exception.ApiException;
import com.cdqidi.httpclientbootstart.util.HttpUtil;
import com.cdqidi.util.JacksonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qd.hik.dto.HkBaseHttpResp;
import com.qd.hik.base.HkBaseTokenDTO;
import com.qd.hik.dto.driver.HkDriverReq;
import com.qd.hik.dto.driver.HkPlateReq;
import com.qd.hik.dto.driver.HkDriverInfoResp;
import com.qd.hik.dto.driver.HkPlateResp;
import com.qd.hik.dto.driver.HkRegisterResp;
import com.qd.hik.token.HkAccessTokenManager;
import com.qd.hik.token.HkAccessToken;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * 2.2 车主
 *
 * @author sjk
 * @create 2024-05-29 14:01
 * @since 1.0.0
 */
@Service
@Slf4j
@RequiredArgsConstructor
@HkAccessToken
public class HkDriverService {
    private final HkBaseTokenDTO hkBaseTokenDTO;
    private final HttpUtil httpUtil;
    private final ObjectMapper objectMapper;

    /**
     * 2.2.1 修改车主信息
     */
    private static final String DRIVER_UPDATE_URL = "/artemis/api/v1/driver/update";
    /**
     * 2.2.2 删除车牌
     */
    private static final String DRIVER_DEL_PLATE_URL = "/artemis/api/v1/driver/delPlate";
    /**
     * 2.2.3 新增车牌
     */
    private static final String DRIVER_ADD_PLATE_URL = "/artemis/api/v1/driver/addPlate";
    /**
     * 2.2.4 注册车主用户
     */
    private static final String DRIVER_REGISTER_URL = "/artemis/api/v1/driver/register";
    /**
     * 2.2.5  注销车主用户
     */
    private static final String DRIVER_DELETE_URL = "/artemis/api/v1/driver/delete";
    /**
     * 2.2.6  获取车主个人信息
     */
    private static final String DRIVER_DRIVER_INFO_URL = "/artemis/api/v1/driver/driverInfo";
    /**
     * 2.2.7 车牌列表查询
     */
    private static final String DRIVER_PLATES_INFO_URL = "/artemis/api/v1/driver/plates";

    /**
     * 2.2.1  修改车主信息
     * @param hkDriverReq 车主信息
     * <p>
     * {
     * "driverId": 1,
     * "phone": "15757166476",
     * "nickName": "asd"
     * }
     *
     * @return 成功 or 失败
     */
    @SneakyThrows
    public HkBaseHttpResp<?> driverUpdate(HkDriverReq hkDriverReq) {
        if (null == hkDriverReq.getDriverId()) {
            throw new ApiException("驾驶人ID不能为空");
        }
        String json = JacksonUtil.getObjectMapper().writeValueAsString(hkDriverReq);

        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + DRIVER_UPDATE_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, HkBaseHttpResp.class);
    }

    /**
     * 2.2.2 删除车牌
     * @param hkPlateReq 车牌信息
     * <p>
     * {
     * "plateNo": "浙 A12345",
     * "plateColor": 1,
     * "driverId": 1
     * }
     *
     * @return 成功 or 失败
     */
    @SneakyThrows
    public HkBaseHttpResp<?> delPlate(HkPlateReq hkPlateReq) {
        String json = JacksonUtil.getObjectMapper().writeValueAsString(hkPlateReq);
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + DRIVER_DEL_PLATE_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, HkBaseHttpResp.class);
    }

    /**
     * 2.2.3 新增车牌，绑定车牌，最多可绑定 5 个车牌
     * @param hkPlateReq 车牌信息
     * <p>
     * {
     * "plateNo": "浙 A12345",
     * "plateColor": 1,
     * "driverId": 1
     * }
     *
     * @return 成功 or 失败
     */
    @SneakyThrows
    public HkBaseHttpResp<?> addPlate(HkPlateReq hkPlateReq) {
        String json = JacksonUtil.getObjectMapper().writeValueAsString(hkPlateReq);
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + DRIVER_ADD_PLATE_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, HkBaseHttpResp.class);
    }

    /**
     * 2.2.4 注册车主用户
     * @param phone 手机号
     * <p>
     * {
     * "phone": "18100192590"
     * }
     * @return 车主ID
     * <p>
     *{
     * "code": "200",
     * "msg": "success",
     * "data": {
     * "driverId": 1
     * }
     */
    @SneakyThrows
    public HkBaseHttpResp<HkRegisterResp> register(String phone) {
        if(!StringUtils.hasLength(phone)){
            throw new ApiException("手机号不能为空");
        }
        String json = objectMapper.createObjectNode().put("phone",phone).toString();
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + DRIVER_REGISTER_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, new TypeReference<>() {
        });
    }

    /**
     * 2.2.5  注销车主用户
     * @param driverId 车主ID
     * <p>
     * {
     * "driverId": 1
     * }
     *
     * @return 成功 or 失败
     *
     */
    @SneakyThrows
    public HkBaseHttpResp<?> delete(Integer driverId) {
        if(null == driverId){
            throw new ApiException("车主ID不能为空");
        }
        String json = objectMapper.createObjectNode().put("driverId",driverId).toString();
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + DRIVER_DELETE_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, HkBaseHttpResp.class);
    }

    /**
     * 2.2.6  获取车主个人信息
     * @param phone 手机号
     * <p>
     * {
     * "phone": "18100192590"
     * }
     * @return 车主信息
     * <p>
     *{
     * "code": "200",
     * "msg": "成功",
     * "data": {
     * "account": "HIK15757166476",
     * "name": "123",
     * "driverId": "1",
     * "balance": 100,
     * "createTime": "yyyy/MM/dd HH:mm:ss",
     * "source": 1
     * }
     * }
     */
    @SneakyThrows
    public HkBaseHttpResp<HkDriverInfoResp> driverInfo(String phone) {
        if(!StringUtils.hasLength(phone)){
            throw new ApiException("手机号不能为空");
        }
        String json = objectMapper.createObjectNode().put("phone",phone).toString();
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + DRIVER_DRIVER_INFO_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, new TypeReference<>() {
        });
    }


    /**
     * 2.2.7 车牌列表查询
     * @param driverId 车主ID
     * <p>
     * {
     * "driverId": 1
     * }
     *
     * @return 车牌列表
     * <p>
     *{
     * "code": "200",
     * "msg": "SUCCESS",
     * "data": {
     * "supportBalanceDeduction": 0,
     * "results": [
     * {
     * "plateId": 280,
     * "plateNo": "津 EDRFTG",
     * "plateColor": 1,
     * "isDefault": 1
     * }
     * ]
     * }
     * }
     */
    @SneakyThrows
    public HkBaseHttpResp<HkPlateResp> plates(Integer driverId) {
        if(null == driverId){
            throw new ApiException("车主ID不能为空");
        }
        String json = objectMapper.createObjectNode().put("driverId",driverId).toString();
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + DRIVER_PLATES_INFO_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, new TypeReference<>() {
        });
    }
}